/*!
 * Quick-dialog jQuery Plugin Ver 1.0.0
 * Support to IE6, IE7, IE8, IE9, Firefox, Chrome, Safari, Opera.
 *
 * Homepage: http://code.google.com/p/realtime-modifier/
 *
 * Copyright 2011, Rainisic
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 * Based on jQuery.js
 * http://jquery.com/
 * Copyright 2011, John Resig
 * Dual licensed under the MIT or GPL Version 2 licenses.
 *
 * Date: Tues Nov 29 11:49:03 2011 +0800
 */
(function($) {

	// Define the dialog configuration.
	var config = null;
	
	$.fn.dialog = function(settings) {
		
		// Default configuration.
		var defaults = {
			trigger:		null,
			title:			null,
			dialogClass:	"quick-dialog-default-dialog",
			titleClass:		"quick-dialog-default-title",
			closeClass: 	"quick-dialog-default-close",
			callback:		null
		};
		
		// Define the config.
		config = $.extend(defaults, settings || {});
		
		// Define the target.
		var target = this;
		
		// Add the dialog box.
		$(target).wrap("<div class='" + config.dialogClass + "'></div>")
			.before("<div class='" + config.titleClass + "'>" +
						"<a>" + config.title + "</a>" +
						"<div class='" + config.closeClass + "'>关闭[x]</div>" +
					"</div>");
		
		// Add the layer and hide it.
		if ($(".quick-dialog-default-layer").size() == 0) {
			
			// Add new dialog layer.
			$("body").append("<div class='quick-dialog-default-layer'></div>");
			$(".quick-dialog-default-layer").css("opacity", 0.5).hide();
		}
		
		// Initialize the dialog.
		$(target).parents("." + config.dialogClass).hide().css("position", "fixed")
			.css("width", $(target).width()).css("height", $(target).height() + $(this).prev("." + config.titleClass).height())
			.css("left", "50%").css("top", "50%")
			.css("margin-left", -$(target).width() / 2 + "px").css("margin-top", -($(target).height() + $(this).prev("." + config.titleClass).height()) / 2 + "px");
		$("." + config.titleClass).bind("selectstart", function() {
			return false;
		});
		
		// Add the default class.
		$("." + config.dialogClass).addClass("quick-dialog-default-dialog");
		$("." + config.titleClass).addClass("quick-dialog-default-title");
		$("." + config.closeClass).addClass("quick-dialog-default-close");
		
		// Switch on.
		$(config.trigger).bind("click", function(){
			$(target).parents("." + config.dialogClass).fadeIn("fast");
			$(".quick-dialog-default-layer").fadeIn("fast");
			console.log(1);
		});
		
		// Switch off.
		$(target).prev().children("." + config.closeClass).bind("click", function() {
			$(target).parents("." + config.dialogClass).fadeOut("fast");
			$(".quick-dialog-default-layer").fadeOut("fast");
		});
		
		// Callback.
		config.callback != null ? config.callback.call(target) : null;
	};
	
	$.fn.resize = function() {
		
		// Define the target.
		var target = this;
			
		// Refresh the dialog size.
		$(target).parents("." + config.dialogClass)
			.animate({
				"width": $(target).width(),
				"height": $(target).height() + $(this).prev("." + config.titleClass).height(),
				"margin-left": -$(target).width() / 2 + "px",
				"margin-top": -($(target).height() + $(this).prev("." + config.titleClass).height()) / 2 + "px"
			}, "fast", "swing");
	};
})(jQuery);